package com.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.beans.AdminInfo;
import com.beans.Orderinfo;
import com.common.Common.EnumOrderState;
import com.jdbc.DBUtil;
import com.utils.PageInfo;
import com.utils.StringUtil;

public class OrderDao {
	private BeanListHandler<Orderinfo> handler = new BeanListHandler<Orderinfo>(Orderinfo.class);
	private String formatStr = "yyyy-MM-dd";

	/**
	 * 获取数据条目
	 * 
	 * @param bigCateId   订单号
	 * @param smallCateId 开始时间
	 * @param goodsName   结束时间
	 * @param orderState  订单状态
	 * @return
	 */
	public int getOrderCount(String orderNo, String beginDate, String endDate, String orderState) {
		StringBuffer sql = new StringBuffer("select count(*) from orderinfo where 1=1 ");

		if (StringUtil.isValidDate(beginDate, formatStr)) {
			sql.append(" and orderDate >='" + beginDate + "'");
		}
		if (StringUtil.isValidDate(endDate, formatStr)) {
			sql.append(" and orderDate <='" + endDate + "'");
		}
		if (!StringUtil.isEmpty(orderNo)) {
			sql.append(" and orderNo like '%" + orderNo + "%'");
		}

		if (!StringUtil.isEmpty(orderState)) {
			sql.append(" and orderState = '" + orderState + "'");
		}
		long count = DBUtil.getScalar(sql.toString());
		return Integer.parseInt(count + "");
	}

	/**
	 * 获取会员列表
	 * 
	 * @param page 分页属性
	 */
	public List<Orderinfo> getOrderList(PageInfo page) {
		return DBUtil.getList("select  * from orderinfo order by id desc limit ?, ? ", Orderinfo.class,
				page.getBeginRow(), page.getPageSize());
	}

	/**
	 * 多条件复合查询订单
	 * 
	 * @param bigCateId   订单号
	 * @param smallCateId 开始时间
	 * @param goodsName   结束时间
	 * @param orderState  订单状态
	 * @param page        分页
	 * @return
	 */
	public List<Orderinfo> getOrderList(String orderNo, String beginDate, String endDate, String orderState,
			PageInfo page) {
		List<Orderinfo> orderList = new ArrayList<Orderinfo>();
		Statement stm = null;
		ResultSet rs = null;

		try {
			Connection conn = DBUtil.getConn();
			stm = conn.createStatement();
			StringBuffer sql = new StringBuffer(" select  * from orderinfo where 1=1 ");

			if (StringUtil.isValidDate(beginDate, formatStr)) {
				sql.append(" and orderDate >='" + beginDate + "'");
			}
			if (StringUtil.isValidDate(endDate, formatStr)) {
				sql.append(" and orderDate <='" + endDate + "'");
			}
			if (!StringUtil.isEmpty(orderNo)) {
				sql.append(" and orderNo like '%" + orderNo + "%'");
			}

			if (!StringUtil.isEmpty(orderState)) {
				sql.append(" and orderState = '" + orderState + "'");
			}

			sql.append(" order by id desc limit " + page.getBeginRow() + ", " + page.getPageSize());
			rs = stm.executeQuery(sql.toString());
			orderList = (List<Orderinfo>) handler.handle(rs);

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(rs, stm);
		}

		return orderList;
	}
	
	/**
	 * 发货
	 * @param id 订单id
	 * @return
	 */
	public int shipments(int id,String sendDate) {
		return DBUtil.update("update orderinfo set orderState=?,sendDate=? where id= ?",EnumOrderState.已发货.getValue(),sendDate, id);
	}
	
	/**
	 * 获取订单商品列表
	 * @param id 订单id
	 * @return
	 */
	public List<Map<String, Object>> getOrderGoods(int id) {
		String sql="select  * from ordergoods where orderId=?";
		return DBUtil.getMapList(sql,id);
	}
	
	/**
	 * 获取订单详情
	 * @param id 订单id
	 * @return
	 */
	public Orderinfo getOrder(int id) {
		String sql = "select * from orderinfo where id=?";
		return DBUtil.getSingleObject(sql, Orderinfo.class, id);
	}
}
